/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.ifrs.poa.dao;

import br.edu.ifrs.poa.database.DBConnection;
import br.edu.ifrs.poa.util.PropertiesManager;
import br.edu.ifrs.poa.model.Noticia;
import br.edu.ifrs.poa.model.Usuario;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

/**
 *
 * @author user
 */
public class NoticiaDAO {

    private static HashMap dados;

    static {
        try {
            dados = new PropertiesManager("sql.properties").readPropertiesFile();
        } catch (IOException ioe) {
            System.out.println(ioe);
        }
    }
    public final static int ID = 1;
    public final static int USUARIO_ID = 2;
    public final static int TITULO = 3;
    public final static int DESCRICAO = 4;
    public final static int LINK = 5;
    public final static int DATA = 6;

    public static void addNews(Noticia news) throws SQLException, ClassNotFoundException, IOException {
        Connection conexao = DBConnection.getInstance();
        String sql = loadSQL("Insert.Noticia");
        PreparedStatement pstmt = conexao.prepareStatement(sql);
        pstmt.setInt(1, news.getUsuario().getId());
        pstmt.setString(2, news.getTitulo());
        pstmt.setString(3, news.getDescricao());
        pstmt.setString(4, news.getLink());
        pstmt.setDate(5, new java.sql.Date(new java.util.Date().getTime()));
        int rs = pstmt.executeUpdate();
        pstmt.close();
    }

    public static ArrayList getNoticias() throws SQLException, ClassNotFoundException, IOException {
        
        ArrayList newsList = new ArrayList();
        Connection conexao = DBConnection.getInstance();
        String sql = loadSQL("SelectAll.Noticia");
        PreparedStatement pstmt = conexao.prepareStatement(sql);

        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            Noticia news = new Noticia();
            news.setId(rs.getInt(ID));
            news.setTitulo(rs.getString(TITULO));
            news.setDescricao(rs.getString(DESCRICAO));
            news.setLink(rs.getString(LINK));
            news.setData(rs.getDate(DATA));

            int id = (rs.getInt(USUARIO_ID));
            news.setUsuario(UsuarioDAO.getUsuarioById(id));

            newsList.add(news);
        }
        pstmt.close();
        return newsList;
    }

    private static String loadSQL(String key) {
        String sql = null;
        sql = (String) dados.get(key);
        return sql;
    }

    public static void main(String[] args) {
//        try {
//            Usuario usuario = UsuarioDAO.getUsuarioById(81);
//            System.out.println(usuario);
//            Noticia news = new Noticia();
//            news.setDescricao("Teste 27");
//            news.setLink("teste 27");
//            news.setTitulo("teste 27");
//            news.setUsuario(usuario);
//            addNews(news);
        ArrayList lista;
        try {
            lista = getNoticias();

            for (Iterator it = lista.iterator(); it.hasNext();) {
                Noticia object = (Noticia) it.next();
                System.out.println(object);
            }

        } catch (SQLException ex) {
            System.out.println("Falhou" + ex);
        } catch (ClassNotFoundException ex) {
            System.out.println("Falhou" + ex);
        } catch (IOException ex) {
            System.out.println("Falhou" + ex);
        }
    }
}
